package com.qiankun.tang.ui.view.adapter.follow;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.common.library.listener.OnAppListener;
import com.common.library.net.items.BaseItem;
import com.common.library.ui.view.adapter.base.BaseRecyclerAdapter;
import com.facebook.drawee.view.SimpleDraweeView;
import com.qiankun.tang.R;
import com.qiankun.tang.beans.ADBean;
import com.qiankun.tang.beans.AuthorBean;
import com.qiankun.tang.beans.VideoSynopsisBean;
import com.qiankun.tang.beans.items.FollowItem;
import com.qiankun.tang.ui.view.HorizontalListWidget;
import com.qiankun.tang.ui.view.SpecialSpacingItemDecoration;
import com.qiankun.tang.ui.view.VideoSynopsisListWidget;
import com.qiankun.tang.ui.view.adapter.holder.BaseLineHolder;

import java.util.List;

/**
 * @author txf
 * @Title
 * @package com.qiankun.video.ui.view.adapter
 * @date 2017/6/9 0009
 */

public class FollowAdapter extends BaseRecyclerAdapter<FollowItem> {

    public FollowAdapter(Context context) {
        super(context);
    }

    public FollowAdapter(Context context, OnAppListener.OnInteractionListener listener) {
        super(context, listener);
    }
    @Override
    public int getItemViewType(int position) {
        return getDatas().get(position).getType();
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case FollowItem.TYPE_0:
                return new FollowViewHolder0(LayoutInflater.from(getContext()).inflate(R.layout.item_follow_0, parent, false));
            case FollowItem.TYPE_1:
                return new FollowViewHolder1(LayoutInflater.from(getContext()).inflate(R.layout.item_follow_1, parent, false));
            case BaseItem.TYPE_LINE:
                return new BaseLineHolder(LayoutInflater.from(getContext()).inflate(R.layout.item_base_line, parent, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof FollowViewHolder0) {
            onBindFollowViewHolder0((FollowViewHolder0) holder, position);
        } else if (holder instanceof FollowViewHolder1) {
            onBindFollowViewHolder1((FollowViewHolder1) holder, position);
        }
    }

    private void onBindFollowViewHolder0(FollowViewHolder0 holder, int position) {
        final AuthorBean bean = (AuthorBean) getDatas().get(position).getBean();
        holder.mIcon.setImageURI(bean.getIconUrl());
        holder.title.setText(bean.getTitle());
        holder.subtitle.setText(bean.getSubtitle());
        holder.mVideoSynopsisListWidget.setData(bean.getList());
        holder.mVideoSynopsisListWidget.addOnItemClickListener(new VideoSynopsisListWidget.OnItemClickListener() {
            @Override
            public void onItemClick(int type, VideoSynopsisBean bean) {
                getListener().onInteraction(FollowItem.TYPE_0,bean);
            }
        });
    }
    private void onBindFollowViewHolder1(FollowViewHolder1 holder, int position) {
        List<ADBean> datas = (List<ADBean>) getDatas().get(position).getBean();
        holder.mHorizontalListWidget.setData(datas,0);
        holder.mHorizontalListWidget.addOnClickListaner(new HorizontalListWidget.OnClickListaner() {
            @Override
            public void onItemClick(ADBean bean) {
                getListener().onInteraction(FollowItem.TYPE_1,bean);
            }
        });
    }

    class FollowViewHolder0 extends RecyclerView.ViewHolder {
        VideoSynopsisListWidget mVideoSynopsisListWidget;
        SimpleDraweeView mIcon;
        TextView follow,title,subtitle;
        public FollowViewHolder0(View itemView) {
            super(itemView);
            itemView.findViewById(R.id.item_hot_1_line).setVisibility(View.GONE);
            itemView.findViewById(R.id.item_hot_1_null).setVisibility(View.GONE);

            mIcon = (SimpleDraweeView) itemView.findViewById(R.id.item_hot_1_icon);
            follow = (TextView) itemView.findViewById(R.id.item_hot_1_follow);
            title = (TextView) itemView.findViewById(R.id.item_hot_1_title);
            subtitle = (TextView) itemView.findViewById(R.id.item_hot_1_subtitle);

            mVideoSynopsisListWidget = (VideoSynopsisListWidget) itemView.findViewById(R.id.item_follow_0_VideoSynopsisLisetWidget);
            mVideoSynopsisListWidget.setItemDecoration(new SpecialSpacingItemDecoration(LinearLayoutManager.HORIZONTAL,dip2px(15),dip2px(4),true));
        }
    }
    class FollowViewHolder1 extends RecyclerView.ViewHolder {
        HorizontalListWidget mHorizontalListWidget;
        public FollowViewHolder1(View itemView) {
            super(itemView);
            mHorizontalListWidget = (HorizontalListWidget) itemView.findViewById(R.id.item_follow_1_HorizontalListWidget);
        }
    }
}
